﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using App.Common;
using QxFramework.Core;
using System;
using DG.Tweening;
public class Chest : SceneMonoBase
{
    [HideInInspector]
    public bool IsPlayerIn;
    [HideInInspector]
    public bool IaActivated;
    [HideInInspector]
    public bool IsDisAppear;

    private float Alph = 0;
    private float AlphPlus = 0;

    public override void OnEnable()
    {
        base.OnEnable();
        IsDisAppear = false;
        StartCoroutine(Init());
    }
    IEnumerator Init()
    {
        yield return 0;
        Alph = 0;
        GetComponent<SpriteRenderer>().color = new Color(1, 1, 1, Alph);
        GetComponent<SpriteRenderer>().sprite = ResourceManager.Instance.Load<Sprite>("Texture/SceneObj/chest_close");
    }
    public override void Update()
    {
        if (!GameMgr.Get<ISceneMonoManager>().GetAllSceneMono()._sceneMonoDatas.ContainsKey(gameObject.name))
        {
            return;
        }
        if (!IsDisAppear)
        {
            if (Alph < 1f)
            {
                AlphPlus = 0.025f;
            }
            Alph += AlphPlus;

            GetComponent<SpriteRenderer>().color = new Color(1, 1, 1, Alph);
            CheckActivated();
            CheckPlayerIsIn(out string playerName);
            CheckPort(playerName);
        }
        base.Update();
    }
    private void CheckActivated()
    {
        IaActivated = GameMgr.Get<IMapManager>().CheckClear(true);
        if (IaActivated)
        {
            if (Alph < 1f)
            {
                AlphPlus = 0.025f;
            }
            Alph += AlphPlus;
        }
        else
        {
            if (Alph < 0)
            {
                Alph = 0;
            }
            else
            {
                Alph -= Time.deltaTime;
            }
        }
        GetComponent<SpriteRenderer>().color = new Color(1, 1, 1, Alph);
    }
    public void CheckPort(string player)
    {
        if (IsPlayerIn)
        {
            GetComponent<SpriteRenderer>().sprite = ResourceManager.Instance.Load<Sprite>("Texture/SceneObj/chest_open");
            bool CantGetItem = true;
            int item = 0;
            while (CantGetItem)
            {
                item = GetRandomItem();
                int ItemCount = 0;
                foreach (int i in GameMgr.Get<IMonoManager>().GetMono(player).ItemList)
                {
                    if (i == item)
                    {
                        ItemCount++;
                    }
                }
                if(ItemCount< Data.Instance.TableAgent.GetInt("AllItem", item.ToString(), "MaxNum"))
                {
                    CantGetItem = false;
                }
            }
            GameMgr.Get<IMonoManager>().GetMono(player).ItemList.Add(item);
            GameMgr.Get<IBuffManager>().AddBuff(GameMgr.Get<IMonoManager>().GetMono(player), Data.Instance.TableAgent.GetInt("AllItem", item.ToString(), "BuffID"), 1);
            GameObject clone = ResourceManager.Instance.Instantiate("Prefabs/Effects/Item");

            clone.transform.position = transform.position;
            clone.GetComponentInChildren<SpriteRenderer>().sprite = ResourceManager.Instance.Load<Sprite>("Texture/Items/"+ Data.Instance.TableAgent.GetString("AllItem", item.ToString(), "Sprite"));

            foreach(MainUI m in FindObjectsOfType<MainUI>())
            {
                m.UpdateItems();
            }

            IsDisAppear = true;
            GetComponent<SpriteRenderer>().DOColor(new Color(0, 0, 0, 0), 0.5f).OnComplete(() => {
                GameMgr.Get<ISceneMonoManager>().RemoveSceneMono(gameObject.name);
            });
        }
    }
    private int GetRandomItem()
    {
        var item = Data.Instance.TableAgent.CollectKey1("AllItem");
        return int.Parse(item[UnityEngine.Random.Range(0, item.Count)]);
    }
    private void CheckPlayerIsIn(out string player)
    {
        IsPlayerIn = false;
        player = "";
        foreach (var item in GameMgr.Get<IPlayerDataManager>().GetPlayerDatas().PlayerDatas)
        {
            if (GetDistance(item.PlayerMono.Position,transform.position) < 3)
            {
                IsPlayerIn = true;
                player = item.PlayerName + "_0"/* + item.PlayerID.ToString()*/;
            }
        }
    }
    private float GetDistance(Vector3 pos1, Vector3 pos2)
    {
        return (pos1.x - pos2.x) * (pos1.x - pos2.x) + (pos1.y - pos2.y) * (pos1.y - pos2.y);
    }
    public override void OnDrawGizmos()
    {
        Gizmos.color = new Color(0, 0, 1, 0.5f);
        base.OnDrawGizmos();
    }
}
